package com.example.makerspace.dao;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.UserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import  java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class UserDAO extends BaseDAO<UserDO>{
    @Resource
    private UserMapper userMapper;

    @PostConstruct
    public void init(){
        this.mapper = userMapper;
    }

    public final static String USERNAME = "username";

    public UserDO selectByUsername(String username){
        return userMapper.selectOne(new QueryWrapper<UserDO>().eq(USERNAME, username));
    }
    public List<UserDO> selectByAllUsername(String username){
        return userMapper.selectList(new QueryWrapper<UserDO>().eq(USERNAME, username));
    }


    public UserDO selectByPhone(String phone) {
        return  userMapper.selectOne(new QueryWrapper<UserDO>().eq("username",phone));
    }

    public UserDO queryIsVerify(String username, Integer competitionParentId) {
        return  userMapper.selectOne(new QueryWrapper<UserDO>().eq("username",username).eq("competition_parent_id",competitionParentId));
    }

    public void deleteByUsername(String username) {
         userMapper.delete(new QueryWrapper<UserDO>().eq("username",username));
    }

    public void updateByUserName(UserDO userDO) {
        UpdateWrapper<UserDO> wrapper = new UpdateWrapper<UserDO>();
        wrapper.eq("username",userDO.getUsername());
         userMapper.update(userDO,wrapper);
    }

    public IPage<UserDO> selectByPage(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageByName(Pagination pagination, String nickname) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("username",nickname);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageByNickName(Pagination pagination, String username) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("nickname",username);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);

    }
    public IPage<UserDO> selectByPageByWaitAuthentication(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("certification_results",0);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
    public IPage<UserDO> selectByPageByNotAuthentication(Pagination pagination) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(UserDO::getCertificationResults);
        wrapper.orderByDesc(UserDO::getCreateTime);
        userMapper.selectList(wrapper);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }
    public IPage<UserDO> selectByPageBySuccessAuthentication(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("is_enterprise_certification",1);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
    public IPage<UserDO> selectByPageByFailAuthentication(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("authentication_type","认证失败");
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
    public List<UserDO> betwenStartAndEndTime(Date startTime) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("create_time",startTime);
        return userMapper.selectList(queryWrapper);
    }

    public IPage<UserDO> selectByPageAndType1(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_user_authentication",1);
        queryWrapper.eq("is_enterprise_certification",0);
        queryWrapper.orderByDesc("create_time");
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageAndType2(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_user_authentication",0);
        queryWrapper.eq("is_enterprise_certification",1);
        queryWrapper.orderByDesc("create_time");
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageAndType3(Pagination pagination) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_authentication",0);
        queryWrapper.orderByDesc("create_time");
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageAndUserType1(Pagination pagination, String username) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_user_authentication",1);
        queryWrapper.eq("is_enterprise_certification",0);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("username",username);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }
    public IPage<UserDO> selectByPageAndUserType2(Pagination pagination, String username) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_user_authentication",0);
        queryWrapper.eq("is_enterprise_certification",1);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("username",username);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }

    public IPage<UserDO> selectByPageAndUserType3(Pagination pagination,String username) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_authentication",0);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.like("username",username);
        return userMapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),queryWrapper);
    }


    public List<UserDO> selectByProvince(String province) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("map_address",province);
        return userMapper.selectList(queryWrapper);
    }

    public List<UserDO> selectBySortPoint() {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("points").orderByDesc("create_time");
        queryWrapper.last("limit 10");
        return userMapper.selectList(queryWrapper);
    }

    public Page<UserDO> getUserList(Page<UserDO> page, String username,String state,String startTime,String endTime) {
        LambdaQueryWrapper<UserDO> qw = new LambdaQueryWrapper<UserDO>();
               qw.eq(StringUtils.isNotEmpty(state), UserDO::getStatus, state).and(userDOLambdaQueryWrapper ->
                               userDOLambdaQueryWrapper.like(StringUtils.isNotEmpty(username),UserDO::getUsername,username).or()
                                       .like(StringUtils.isNotEmpty(username),UserDO::getNickname,username).or()
                                       .like(StringUtils.isNotEmpty(username),UserDO::getEntName,username).or()
                                       .like(StringUtils.isNotEmpty(username),UserDO::getIndividual,username))
                .le(UserDO::getCreateTime,endTime).ge(UserDO::getCreateTime,startTime)
                .orderByDesc(UserDO::getCreateTime);
        return  userMapper.selectPage(page,qw);
    }

    public Page<UserDO> getUserListByPage(Page<UserDO> page) {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return  userMapper.selectPage(page,wrapper);
    }

    public Page<UserDO> getByPageUserNameAndState(Page<UserDO> page, String username, String state) {
        LambdaQueryWrapper<UserDO> qw = new LambdaQueryWrapper<UserDO>();
                qw.eq(StringUtils.isNotEmpty(state), UserDO::getStatus, state).and(userDOLambdaQueryWrapper ->
                                userDOLambdaQueryWrapper.like(StringUtils.isNotEmpty(username),UserDO::getUsername,username).or()
                                        .like(StringUtils.isNotEmpty(username),UserDO::getNickname,username).or()
                                        .like(StringUtils.isNotEmpty(username),UserDO::getEntName,username).or()
                                        .like(StringUtils.isNotEmpty(username),UserDO::getIndividual,username))
                        .orderByDesc(UserDO::getCreateTime);

        return  userMapper.selectPage(page,qw);

    }

    public List<UserDO> selectBySort() {
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("points").orderByDesc("create_time");
        return mapper.selectList(wrapper);
    }

    public Page<UserDO> getUserListByTime(Page<UserDO> page, String startTime, String endTime) {
        LambdaQueryWrapper<UserDO> qw = new LambdaQueryWrapper<UserDO>();
              qw.le(UserDO::getCreateTime,endTime).ge(UserDO::getCreateTime,startTime)
                .orderByDesc(UserDO::getCreateTime);
        return  userMapper.selectPage(page,qw);
    }

    public Page<UserDO> getUserListByState(Page<UserDO> page, String state, String startTime, String endTime) {
        LambdaQueryWrapper<UserDO> qw = new LambdaQueryWrapper<UserDO>();
        qw.eq(StringUtils.isNotEmpty(state), UserDO::getStatus, state)
                .le(UserDO::getCreateTime,endTime).ge(UserDO::getCreateTime,startTime)
                .orderByDesc(UserDO::getCreateTime);
        return  userMapper.selectPage(page,qw);
    }

    public List<UserDO> selectByName(String name) {
        LambdaQueryWrapper<UserDO> qw = new LambdaQueryWrapper<UserDO>();
        qw.eq(StringUtils.isNotEmpty(name),UserDO::getUsername,name);
        return  mapper.selectList(qw);
    }
}
